package org.example.services.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.Exception.RollBackException;
import org.example.dao.MenuDao;
import org.example.dao.RoleDao;
import org.example.domain.Menu;
import org.example.domain.Role;
import org.example.domain.response.ResponseResult;
import org.example.services.MenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.ArrayUtils;
import org.thymeleaf.util.ListUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
@Transactional  //事务回滚
public class MenuServiceImpl implements MenuService {

    @Resource
    private MenuDao menuDao;
    @Resource
    private RoleDao roleDao;

    @Override
    public ResponseResult getAllMenus() {
        try {
            List<Menu> menus = menuDao.getMenus();
            log.info("获取全部权限信息成功");
            return ResponseResult.success(menus);
        } catch (Exception e) {
            e.printStackTrace();
            log.warn("获取全部权限信息失败");
            return ResponseResult.failed("获取权限信息失败");
        }
    }

    @Override
    public ResponseResult getMenusByRoleId(int roleId) {
        try {
            Role role = roleDao.selectById(roleId);
            if (role == null){
                log.info("{}角色ID不存在",roleId);
                return ResponseResult.failed("该角色ID不存在");
            }
            List<Menu> menu = menuDao.selectByRoleId(roleId);
            log.info("获取{}角色的权限信息为{}",role.getName(),menu);
            return ResponseResult.success(menu);
        } catch (Exception e) {
            e.printStackTrace();
            log.warn("获取{}角色权限信息失败",roleId);
            return ResponseResult.failed("获取角色权限信息失败");
        }
    }

    @Override
    public ResponseResult updateRoleMenus(int roleId, List<Integer> menuList) {
        try {
            if(ListUtils.isEmpty(menuList)){
                return ResponseResult.failed("权限列表不能为空");
            }
            Role role = roleDao.selectById(roleId);
            if (role == null){
                log.info("{}角色ID不存在",roleId);
                return ResponseResult.failed("该角色ID不存在");
            }
            for (Integer menu : menuList) {
                if (menuDao.selectById(menu) == null) {
                    return ResponseResult.failed("权限ID中有有错误");
                }
            }
            menuDao.deleteMenusByRoleId(roleId);
            menuDao.saveMenusByRoleId(roleId,menuList);
            log.info("修改{}角色权限成功，权限：{}",role.getName(),menuList);
            return ResponseResult.success("修改角色权限信息成功",null);
        } catch (Exception e) {
            e.printStackTrace();
            log.warn("修改角色权限失败");
            throw new RollBackException("修改角色权限失败");
        }
    }

    @Override
    public ResponseResult deleteRoleMenus(int roleId) {
        try {
            Role role = roleDao.selectById(roleId);
            if(role == null){
                log.info("清除角色权限：{}角色不存在",roleId);
                return ResponseResult.failed("角色不存在，请刷新重试");
            }
            List<Menu> menus = menuDao.selectByRoleId(roleId);
            if(ListUtils.isEmpty(menus)){
                log.info("清除角色权限：{}:{}角色没有任何权限",role.getRoleId(),role.getName());
                return ResponseResult.failed("角色没有任何权限");
            }
            menuDao.deleteMenusByRoleId(roleId);
            log.info("{}：{}角色的权限清理成功",role.getRoleId(),role.getName());
            return ResponseResult.success("角色权限清理成功",null);
        } catch (Exception e) {
            e.printStackTrace();
            log.warn("{}的权限清除失败",roleId);
            throw new RollBackException("权限清除失败");
        }
    }
}
